import Vue from 'vue'
import Router from 'vue-router'
import key from '@/components/js/key.js'
Vue.use(Router)
import Layout from '@/layout'

// icon: 'jssq' 印章 jsfl 模版
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
     meta: { title: '登陆' },
  },
  {
    path: '/onepage',
    name:'onepage',
    component: () => import('@/views/user/onepage'),
    hidden: true
  },

  {
    path: '/class',
    name:'class',
    component: () => import('@/views/class/list'),
    meta:{
      title:"疫苗公示",
       icon: 'fwfl'
    },
    hidden: false
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: "/",
        name: "class",
        component: () => import('@/views/service/class'),
        meta: {
          title: '疫苗通知',
          icon: 'fwfl'
        }
      },
    ]
  },
  {
    path: '/report',
    name:'report',
    component: () => import('@/views/user/report'),
    hidden: true
  },
  {
    path: '/reg',
    name:'reg',
    component: () => import('@/views/user/reg'),
    // meta: { title: '十堰市二价宫颈疫苗预约' },
    hidden: true
  },
  {
    path: '/pet',
    name:'pet',
    component: () => import('@/views/user/pet'),
    // meta: { title: '宠物医疗！' },
    hidden: true
  },
 {
    path: '/list',
    name:'list',
    component: () => import('@/views/user/list'),
    hidden: true
  },
//宠物预约的列表
{
    path: '/petlist',
    name:'petlist',
    component: () => import('@/views/service/petlist'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/history',
    component: Layout,
    redirect: "/user/history",
    meta: {
      title: '模版管理',
      icon: 'el-icon-s-custom'
    },
    children: [{
      path: "history",
      name: "history",
      component: () => import('@/views/user/history'),
      meta: {
        title: '历史发送消息',
        icon: 'yhgl'
      },
    }, ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: "/",
        name: "class",
        component: () => import('@/views/service/class'),
        meta: {
          title: '通知公告群发',
          icon: 'fwfl'
        }
      },
    ]
  },

  {
    path: '/user',
    component: Layout,
    redirect: '/user/user',
    children: [{
      hidden: true,
      path: 'user',
      name: 'user',
      component: () => import('@/views/user/list'),
      meta: {
        title: '个人中心',
         icon: 'fwfl'
      },

    }]
  },
  {
    path: '/index',
    component: Layout,
    redirect: "/user/index",
    meta: {
      title: '模版管理',
      icon: 'el-icon-s-custom'
    },
    children: [{
      path: "index",
      name: "index",
      component: () => import('@/views/user/index'),
      meta: {
        title: '模版列表',
        icon: 'yhgl'
      },
    }, ]
  },


  // {
  //   path: '/organization',
  //   component: Layout,
  //   redirect: "/organization/list",
  //   meta: {
  //     title: '机构管理',
  //     icon: 'jggl'
  //   },
  //   children: [{
  //       path: "list",
  //       name: "list",
  //       component: () => import('@/views/organization/list'),
  //       meta: {
  //         title: '机构列表',
  //         icon: 'jglb'
  //       }
  //     },
  //     {
  //       path: "reg",
  //       name: "reg",
  //       component: () => import('@/views/organization/reg'),
  //       meta: {
  //         title: '机构注册',
  //         icon: 'jgzc'
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: '/service',
  //   component: Layout,
  //   redirect: "/service/list",
  //   meta: {
  //     title: '',
  //     icon: 'fwgl'
  //   },
  //   children: [
  //   {
  //     path: "class",
  //     name: "class",
  //     component: () => import('@/views/service/class'),
  //     meta: {
  //       title: '模版消息群发',
  //       icon: 'fwfl'
  //     }
  //   },
  //     {
  //       path: "apply",
  //       name: "apply",
  //       component: () => import('@/views/service/apply'),
  //       meta: {
  //         title: '申请管理',
  //         icon: 'sqgl'
  //       }
  //     },
  //     {
  //       path: "list",
  //       name: "list",
  //       component: () => import('@/views/service/index'),
  //       meta: {
  //         title: '服务列表',
  //         icon: 'fwlb'
  //       }
  //     },
  //     {
  //       hidden: true,
  //       path: "updata",
  //       name: "updata",
  //       component: () => import('@/views/service/updata'),
  //       meta: {
  //         title: '添加服务',
  //         icon: 'dashboard'
  //       }
  //     },
  //     {
  //       hidden: true,
  //       path: "add",
  //       name: "add",
  //       component: () => import('@/views/service/add'),
  //       meta: {
  //         title: '添加服务',
  //         icon: 'dashboard'
  //       }
  //     },

  //     {
  //       path: "order",
  //       name: "order",
  //       component: () => import('@/views/service/order'),
  //       meta: {
  //         title: '订单列表',
  //         icon: 'el-icon-coin'
  //       }
  //     },
  //     {
  //       path: "back",
  //       name: "back",
  //       component: () => import('@/views/service/back'),
  //       meta: {
  //         title: '退款管理',
  //         icon: 'tkgl'
  //       }
  //     },
  //   ]
  // },
  {
    path: "/sub",
    component: Layout,
    redirect: "/sub/area",
    meta: {
      title: '杂项管理',
      icon: 'zxgl'
    },
    children: [{
      path: 'center',
      name: 'center',
      component: () => import('@/views/admin/index'),
      meta: {
        title: '个人中心',
        icon: 'el-icon-s-custom'
      },
    }, ]
  },
  // {
  //   path: '/user',
  //   component: Layout,
  //   redirect: "/user/list",
  //   meta: {
  //     title: '模版管理',
  //     icon: 'el-icon-s-custom'
  //   },
  //   children: [{
  //     path: "list",
  //     name: "list",
  //     component: () => import('@/views/user/list'),
  //     meta: {
  //       title: '323输机',
  //       icon: 'yhgl'
  //     },
  //   }, ]
  // },

]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
//路由前置守卫（垃圾重写）
router.beforeEach((to, from, next) => {
  if (to.fullPath != '/login'  ) {
    if(to.name == 'onepage' ||  to.name == 'list' || to.name == 'reg' ||to.name == 'pet'||to.name == 'petlist' ){
      next()
    }else{
      const admin = JSON.parse(sessionStorage.getItem('admin'));
      console.log('admin:', admin);
      if (admin == null) {
        return next('/login')
      } else {
        next()
      }
    }
  }
  next()
})
